package com.jzo2o.market.controller.inner;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.market.CouponApi;
import com.jzo2o.api.market.dto.request.CouponUseBackReqDTO;
import com.jzo2o.api.market.dto.request.CouponUseReqDTO;
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
import com.jzo2o.api.market.dto.response.CouponDTO;
import com.jzo2o.api.market.dto.response.CouponUseResDTO;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author itlxc
 * @since 2024-11-06
 */
@RestController("innerCouponController")
@RequestMapping("/inner/coupon")
@Api(tags = "内部接口 - 优惠券相关接口")
@Slf4j
@RequiredArgsConstructor
public class InnerCouponController implements CouponApi {

    private final ICouponService couponService;

    @Override
    @GetMapping("/getAvailable")
    @ApiOperation("获取可用的优惠券列表")
    @ApiImplicitParam(name = "totalAmount", value = "总金额，单位分", required = true, dataTypeClass = BigDecimal.class)
    public List<AvailableCouponsResDTO> getAvailable(@RequestParam("totalAmount") BigDecimal totalAmount) {
        return couponService.getAvailable(totalAmount);
    }

    @Override
    @PostMapping("/use")
    @ApiOperation("使用优惠券，并返回优惠金额")
    public CouponUseResDTO use(@RequestBody CouponUseReqDTO couponUseReqDTO) {
        return couponService.use(couponUseReqDTO);
    }

    @Override
    @PostMapping("/useBack")
    @ApiOperation("优惠券退回")
    public void useBack(@RequestBody CouponUseBackReqDTO couponUseBackReqDTO) {
        couponService.useBack(couponUseBackReqDTO);
    }

    @Override
    @GetMapping("/{id}")
    @ApiOperation("根据订单id查询优惠券")
    public CouponDTO getByOrderId(@PathVariable("id") Long id) {
        Coupon coupon = couponService.getById(id);
        return BeanUtil.copyProperties(coupon, CouponDTO.class);
    }
}
